import { parse } from 'url';

// mock tableListDataSource
const tableListDataSource = [
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.52",
    fef: "104",
    name: "张丽丽",
    updatedAt: "2019-04-12T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.82",
    fef: "112",
    name: "程鹏",
    updatedAt: "2019-04-05T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.01",
    fef: "115",
    name: "张东",
    updatedAt: "2019-04-01T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.18",
    fef: "110",
    name: "蔡全明",
    updatedAt: "2019-03-25T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.82",
    fef: "104",
    name: "刘丽娜",
    updatedAt: "2019-03-12T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.51",
    fef: "108",
    name: "童谣",
    updatedAt: "2019-03-04T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.62",
    fef: "125",
    name: "董戈",
    updatedAt: "2019-03-01T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.72",
    fef: "113",
    name: "玛丽莎",
    updatedAt: "2019-02-22T14:00:00.000Z",
  },

  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.78",
    fef: "124",
    name: "刘斌",
    updatedAt: "2019-02-15T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.74",
    fef: "125",
    name: "陆大坤",
    updatedAt: "2019-02-11T14:00:00.000Z",
  },
  {
    url: "http://cdn.imgs.3vyd.com/xh/1556004199968.jpg",
    callNo: "11101",
    fev1: "2.88",
    fef: "129",
    name: "郑倩华",
    updatedAt: "2019-02-02T14:00:0.000Z",
  },

];
// for (let i = 0; i < 46; i += 1) {
//   tableListDataSource.push({
//     key: i,
//     disabled: i % 6 === 0,
//     href: 'https://ant.design',
//     avatar: [
//       'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
//       'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
//     ][i % 2],
//     name: `TradeCode ${i}`,
//     title: `一个任务名称 ${i}`,
//     owner: '曲丽丽',
//     desc: '这是一段描述',
//     callNo: Math.floor(Math.random() * 1000),
//     status: Math.floor(Math.random() * 10) % 4,
//     updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     progress: Math.ceil(Math.random() * 100),
//   });
// }

function getFeiGongneng(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  return res.json(result);
}

export default {
  'GET /api/feigongneng': getFeiGongneng,
};
